package edu.pku.sei.transformation.pattern;

import edu.pku.sei.mdabase.infrastructure.graph.Graph;
import edu.pku.sei.mdabase.infrastructure.runtime.Context;


public class TPatternGraph extends Graph<TPatternNode,TPatternEdge>  implements IPattern{

	static private int unnamedCount = 0;
	
	static public String allocateNameFor(Context context){
		return "__UNNAMED__"+(unnamedCount++);
	}
	
	
	public void registVariableToContext(Context context){
		for(TPatternNode pn : getNodes()){
			if(pn.getName()==null)
				pn.setName(allocateNameFor(context));
			context.registVariable(pn.getName(), pn.getType(),true, getDomainName());
		}
		for(TPatternEdge pe : getEdges()){
			if(pe.getName()==null)
				pe.setName(allocateNameFor(context));
			context.registVariable(pe.getName(), pe.getType(),true, getDomainName());
		}
	}
	
	public boolean containsNode(String name){
		for(TPatternNode n : getNodes()){
			if(n.getName().equals(name))
				return true;
		}
		return false;
	}
	
	public TPatternNode getNodeByName(String name)
	{
		for(TPatternNode n : getNodes()){
			if(n.getName().equals(name))
				return n;
		}
		return null;
	}

	private String domainName;

	public void setDomainName(String domainName)
	{
		this.domainName = domainName;
	}


	@Override
	public String getDomainName()
	{
		return domainName;
	}
	
}
